package com.example.demo.model;
import java.util.ArrayList;

/**
 * @author ：Docin
 * @date ：Created in 2020/9/11 10:25
 * @description：终端的实体类，包含位置、职级、类型、速度、能量、路由表等字段
 * @modified By：
 */
public class Node {
    Integer id;     //节点ID
    Integer rank;   //指挥等级
    double x;      //初始位置x
    double y;      //初始位置y
    double range;   //通信范围
    double power;  //信号发射功率
    String near;  //邻接表，数组和字符串转换

    //分簇算法需要的变量
    //private double dc;  //节点的度？
    private double velocity_x = 0;
    private double velocity_y = 0;


    //其他变量
    public ArrayList<LinkNode> neibNodes = new ArrayList<LinkNode>();   //邻节点
    public Router[] routers;                                    //路由表

    //常量
    final static int NO_CONNECTION = -1;
    final static int F_ASYM = 0;
    final static int B_ASYM = 1;
    final static int SYM = 2;

    public void setRouter(int rid, Node next, int metric, int state1){
        for (int i = 0; i < routers.length; i++) {
            if (routers[i].getDstId() == rid){
                this.routers[i].setNext(next);
                this.routers[i].setNextId(next.getId());
                this.routers[i].setMetric(metric);
                this.routers[i].setState1(state1);
            }
        }

    }

    //返回该节点一跳连接的节点ID及链接状态
    public int[][] getLinkedbNodes(int index){
        int nd[][] = new int[3][10];
        for (int i = 0; i < this.neibNodes.size(); i++) {
            LinkNode tmp = this.neibNodes.get(i);
            if (tmp.getLinkstate() == F_ASYM || tmp.getLinkstate() == SYM){
                nd[0][0] = nd[0][0] + 1;
                nd[0][nd[0][0]] = tmp.getNode().getId();
                nd[1][nd[0][0]] = tmp.getLinkstate();
                nd[2][nd[0][0]] = i;
            }
        }
        nd[1][0] = this.id;
        nd[2][0] = index;
        return nd;
    }

    public void receiveRoutInfo(int[][] nd, Node[] allnodes){
        for (int i = 0; i < this.routers.length; i++) {
            Router tmpr = this.routers[i];
            if (tmpr.getNextId() == -1 && tmpr.getDstId() != nd[1][0]){
                for (int j = 1; j < nd[0][0]+1; j++) {
                    if (tmpr.getDstId() == nd[0][j]){
                        if (nd[1][j] == SYM){
                            tmpr.setNext(allnodes[nd[2][0]]);   //id不能当脚标！！！！！
                            tmpr.setNextId(nd[1][0]);
                            tmpr.setMetric(2);
                            tmpr.setState1(SYM);
                        }
                        else if (nd[1][j] == F_ASYM){
                            tmpr.setNext(allnodes[nd[2][0]]);
                            tmpr.setNextId(nd[1][0]);
                            tmpr.setMetric(2);
                            tmpr.setState1(F_ASYM);
                        }
                    }
                }
            }
            else if (tmpr.getNextId2() == -1 && tmpr.getDstId() != this.id && tmpr.getDstId() != nd[1][0]){
                for (int j = 1; j < nd[0][0]+1; j++) {
                    if (tmpr.getDstId() == nd[0][j]) {
                        if (nd[1][j] == F_ASYM) {
                            tmpr.setNext2(allnodes[nd[2][0]]);
                            tmpr.setNextId2(nd[1][0]);
                            tmpr.setMetric2(2);
                            tmpr.setState2(F_ASYM);
                        }
                        else if (nd[1][j] == SYM) {
                            tmpr.setNext2(allnodes[nd[2][0]]);
                            tmpr.setNextId2(nd[1][0]);
                            tmpr.setMetric2(2);
                            tmpr.setState2(SYM);
                        }
                    }
                }
            }
            if (tmpr.getState1() == F_ASYM && tmpr.getMetric() == 2){
                for (int j = 1; j < nd[0][0]+1; j++) {
                    if (tmpr.getDstId() == nd[0][j] && nd[1][j] == SYM){
                        tmpr.setNext(allnodes[nd[2][0]]);
                        tmpr.setNextId(nd[1][0]);
                        tmpr.setMetric(2);
                        tmpr.setState1(SYM);
                    }
                }
            }
            if (tmpr.getState2() == SYM && tmpr.getMetric2() == 2){
                for (int j = 1; j < nd[0][0]+1; j++) {
                    if (tmpr.getDstId() == nd[0][j] && nd[1][j] == F_ASYM){
                        tmpr.setNext2(allnodes[nd[2][0]]);
                        tmpr.setNextId2(nd[1][0]);
                        tmpr.setMetric2(2);
                        tmpr.setState2(F_ASYM);
                    }
                }
            }
        }
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getRank() {
        return rank;
    }

    public void setRank(Integer rank) {
        this.rank = rank;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getRange() {
        return range;
    }

    public void setRange(double range) {
        this.range = range;
    }

    public double getPower() {
        return power;
    }

    public void setPower(double power) {
        this.power = power;
    }

    public String getNear() {
        return near;
    }

    public void setNear(String near) {
        this.near = near;
    }

    public double getVelocity_x() {
        return velocity_x;
    }

    public void setVelocity_x(double velocity_x) {
        this.velocity_x = velocity_x;
    }

    public double getVelocity_y() {
        return velocity_y;
    }

    public void setVelocity_y(double velocity_y) {
        this.velocity_y = velocity_y;
    }
}
